package javafx.webview.test;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;

/**
 * 作者 zxj<br>
 * 时间 2017/10/20 19:39
 * 说明 ...
 */
public class App extends Application {
    private final WebView webView = new WebView();
    private final WebEngine engine = webView.getEngine();
    private String path = "app.html";
    @Override
    public void start(Stage primaryStage) throws Exception {
        VBox hBox = new VBox();
        hBox.getChildren().add(webView);
        Button button = new Button("a");
        button.setOnAction((e) -> engine.executeScript("showAlert()"));
        hBox.getChildren().add(button);
        Scene scene = new Scene(hBox);
        load();
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    private void load(){
        JSObject window = (JSObject) engine.executeScript("window");
        window.setMember("system",new SystemService());
        engine.load(getClass().getClassLoader().getResource(path).toExternalForm());

        engine.setOnAlert(new EventHandler<WebEvent<String>>() {
            @Override
            public void handle(WebEvent<String> event) {
                System.out.println(event.getData());
            }
        });





    }

    public static void main(String[] args) {
        launch(args);
    }
}
